package org.jeecg.desen.file;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 执行文件迁移
 */
@Slf4j
public class DFSFileMigrate {

    public static String dfsMigrate(String shellPath, String targetDfsIP, String schema, String tabName, String date, String type, String partition) {
        try {
            String shellZl = shellPath + " " + targetDfsIP + " " + schema + " " + tabName
                    + " " + date + " " + type + " " + partition;
            log.info("shell 脚本 指令=" + shellZl);
            Process ps = Runtime.getRuntime().exec(shellZl);
            String inputStr = getShellResult(ps.getInputStream());
            log.info("getShellResult getInputStream " + inputStr);
            String error = getShellResult(ps.getErrorStream());
            log.info("getShellResult getErrorStream " + error);

            int nt = ps.waitFor();
            log.info("waitFor end = " + nt);

            if (StringUtils.isNotEmpty(error) || inputStr.indexOf("失败") != -1) {
                return "执行错误:" + inputStr + ", error =" + error;
            }
            if (0 == nt) {
                return "";
            }
            return "执行文件迁移错误!";
        } catch (Exception e) {
            String error = "执行文件迁移异常,shellPath = " + shellPath + ",schema=" + schema +
                    ",tabName=" + tabName + ",targetDfsIP=" + targetDfsIP + ",date=" + date + ",type=" + type + ",partition=" + partition;
            log.error(error, e);
            return error;
        }
    }

    /**通过线程读取缓冲池里的数据,防止线程堵塞 */
    private static void threasShellResult(InputStream inputStream){
        new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer sb = new StringBuffer();
                try {
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line).append("\n");
                    }
                    log.info(sb.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (br != null) {
                        try {
                            br.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();

    }

    private static String getShellResult(InputStream inputStream) {
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer sb = new StringBuffer();
        try {
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}
